﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InWms
{
    public partial class AssignLocationDetailForm : BaseForm
    {
        #region 成员变量
        public BilInBill InBill { get; set; }
        public BilInBillDetail InBillDetail { get; set; }
        public StoGoodsStock GoodsStock { get; set; }
        #endregion

        #region 构造函数
        public AssignLocationDetailForm(BilInBill inBill, BilInBillDetail inBillDetail)
        {
            InitializeComponent();
            InBill = inBill;
            InBillDetail = inBillDetail;
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

        }
        private void InitControl()
        {
            //初始化库位,根据货物编号获得库位列表
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            WhLocation queryRecord = new WhLocation();
            queryRecord.StoreGoodsCode = InBillDetail.GoodsCode;
            if (InBill.IBType.Trim() == "1")
            {
                queryRecord.WarehouseCode = InBill.WarehouseCode;
            }
            else
            {
                queryRecord.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
            }
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            List<WhLocation> locationList = client.QueryLocationList(queryRecord, ref pIndex).ToList();
            WhLocation tempLocation = new WhLocation();
            if (InBill.IBType.Trim() == "1")
            {
                tempLocation.WarehouseCode = InBill.WarehouseCode;
                tempLocation.WarehouseName = InBill.WarehouseName;
            }
            else
            {
                tempLocation.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
                tempLocation.WarehouseName = MainForm.CurrentUser.WarehouseName;
            }
           
            tempLocation.LocationCode = "temp";
            tempLocation.LocationName = "临时缓存区";

            if (locationList != null)
            {
                locationList.Add(tempLocation);
            }
            else
            {
                locationList = new List<WhLocation>(); 
                locationList.Add(tempLocation);
            }
            cmbLocation.DataSource = locationList;
            //初始化入库单明细内容
            tbGoodsCode.Text = InBillDetail.GoodsCode;
            tbBarCode.Text = InBillDetail.GoodsBarCode;
            tbGoodsName.Text = InBillDetail.GoodsName;
            tbSingleGoodsCode.Text = InBillDetail.SingleGoodsCode;
            nudPackageQty.Value = 0;
            nudPieceQty.Value = 0;
            nudPackageQty.Maximum = Convert.ToDecimal(InBillDetail.ReceiveGoodsPackageQty);
            nudPieceQty.Maximum = Convert.ToDecimal(InBillDetail.ReceiveGoodsPieceQty);
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (cmbLocation.SelectedIndex == -1)
            {
                MessageBox.Show("请选择库位！");
                return;
            }

            if (nudPackageQty.Value == 0 && nudPieceQty.Value == 0)
            {
                MessageBox.Show("整箱和零数不能同时为0！");
                return;
            }
            GoodsStock = new StoGoodsStock();
            WhLocation selectlocation = cmbLocation.SelectedItem as WhLocation;
            //分配后的库存信息
            //库位信息
            GoodsStock.WarehouseCode = selectlocation.WarehouseCode;
            GoodsStock.WarehouseName = selectlocation.WarehouseName;
            GoodsStock.AreaCode = selectlocation.AreaCode;
            GoodsStock.AreaName = selectlocation.AreaName;
            GoodsStock.ShelfCode = selectlocation.ShelfCode;
            GoodsStock.ShelfName = selectlocation.ShelfName;
            GoodsStock.LocationCode = selectlocation.LocationCode;
            GoodsStock.LocationName = selectlocation.LocationName;
            //货物信息
            GoodsStock.GoodsCode = InBillDetail.GoodsCode;
            GoodsStock.GoodsBarCode = InBillDetail.GoodsBarCode;
            GoodsStock.SingleGoodsCode = InBillDetail.SingleGoodsCode;
            GoodsStock.GoodsName = InBillDetail.GoodsName;
            GoodsStock.GoodsSpec = InBillDetail.GoodsSpec;
            GoodsStock.CostPrice = InBillDetail.PiecePrice;
            GoodsStock.GoodsUnit = "";
            GoodsStock.PackageQty = Convert.ToInt32(nudPackageQty.Value);
            GoodsStock.PieceQty = Convert.ToInt32(nudPieceQty.Value);
            GoodsStock.GoodsTotalQty = GoodsStock.PieceQty + GoodsStock.PackageQty * GoodsStock.GoodsSpec;
            //入库单信息
            GoodsStock.IBCode = InBill.Id;
            GoodsStock.IBDate = InBill.ReceiveDate;
            GoodsStock.IBPlanCode = InBill.IBPlanCode;


            ServiceReferenceZwms.ServiceZwmsClient client
            = new ServiceReferenceZwms.ServiceZwmsClient();
            BaseGood queryRecord = new BaseGood();
            queryRecord.Id = GoodsStock.GoodsCode;
            queryRecord = client.QueryGoods(queryRecord);
            if (queryRecord != null)
            {
                GoodsStock.CategoryCode = queryRecord.CategoryCode;
                GoodsStock.CategoryName = queryRecord.CategoryName;
            }

            //入库单中的货物数信息
            InBillDetail.ReceiveGoodsPackageQty -= GoodsStock.PackageQty;
            InBillDetail.ReceiveGoodsPieceQty -= GoodsStock.PieceQty;
            InBillDetail.ReceiveGoodsQty -= GoodsStock.GoodsTotalQty;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
